package br.inf.mensageiro.view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;

import br.inf.mensageiro.main.Mensageiro;
import br.inf.mensageiro.util.GUIUtil;
import br.inf.mensageiro.util.Imagem;
import br.inf.mensageiro.util.GUIUtil.CompCenterCords;
import br.inf.mensageiro.view.listener.ChatGUIUAListener;

public class WindowView extends JFrame {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -828273965248093031L;
	
	private JTextPane ma;
	private JTextArea ua;
	private String id;
	private OutputStream out;
	private String remoteUserName;
	private boolean remoteUserClosed=true;
	
	public WindowView(final Socket socket){
		try{
			out=socket.getOutputStream();
		}catch(IOException ioe){
			ioe.printStackTrace();
		}		
		Container dp=getContentPane();
		dp.setLayout(new BorderLayout());		
		JSplitPane jsp=new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		jsp.setDividerLocation(300);
		final WindowView thisChat=this;
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				try{
					out.write(-1);
				}catch(IOException ioe){
					ioe.printStackTrace();
				}
				if(remoteUserClosed){
					thisChat.dispose();
					InetAddress remoteUserAdd=socket.getInetAddress();
					String ip=remoteUserAdd.getHostAddress();
					try{
						out.close();
						socket.close();
					}catch(IOException ioe){
						ioe.printStackTrace();
					}
					List<String> chatIds=Mensageiro.ipwindowsViewIdMap.get(ip);
					chatIds.remove(id);
					Mensageiro.windowViewMap.remove(id);	
				}
			}
		});
		
		ma=new JTextPane();
		ma.setEditable(false);
		JScrollPane jspma=new JScrollPane(ma);		
		jspma.setBounds(0, 0, 250, 300);
		jsp.add(jspma,JSplitPane.TOP);
		
		
		ua=new JTextArea();
		ua.setDragEnabled(false);
		ua.setLineWrap(true);
		ua.setWrapStyleWord(true);
		ua.addKeyListener(new ChatGUIUAListener(this));
		JScrollPane jspua=new JScrollPane(ua);		
		jspua.setBounds(0, 300, 250, 100);	
		jsp.add(jspua,JSplitPane.BOTTOM);
		
		dp.add(jsp,BorderLayout.CENTER);	
		
		JMenuBar menuBar=new JMenuBar();		
		setJMenuBar(menuBar);
		
		setIconImage(Imagem.ICON_BALLOON.getImage());				
		setSize(260, 450);		
		CompCenterCords cords=GUIUtil.getCompCenterCords(260, 450);
		setLocation(cords.getX(), cords.getY());
		
	}

	public JTextPane getMa() {
		return ma;
	}

	public void setMa(JTextPane ma) {
		this.ma = ma;
	}

	public JTextArea getUa() {
		return ua;
	}

	public void setUa(JTextArea ua) {
		this.ua = ua;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public OutputStream getOut() {
		return out;
	}

	public void setOut(OutputStream out) {
		this.out = out;
	}

	public String getRemoteUserName() {
		return remoteUserName;
	}

	public void setRemoteUserName(String remoteUserName) {
		this.remoteUserName = remoteUserName;
	}

	public boolean isRemoteUserClosed() {
		return remoteUserClosed;
	}

	public void setRemoteUserClosed(boolean remoteUserClosed) {
		this.remoteUserClosed = remoteUserClosed;
	}
			
}
